#!/usr/local/jruby/bin/jruby
require 'java'

include_class "com.sikanrong.RubyGLWrapper"

include_class "javax.swing.JFrame"
include_class "java.awt.BorderLayout"
include_class "com.sun.opengl.util.Animator"
include_class "javax.media.opengl.GL"
include_class "javax.media.opengl.GLCanvas"
include_class "javax.media.opengl.GLCapabilities"
include_class "javax.media.opengl.GLAutoDrawable"
include_class "javax.media.opengl.GLDrawableFactory"
include_class "javax.media.opengl.GLEventListener"
include_class "com.sun.opengl.util.GLUT"



class RubyGLListener < RubyGLWrapper
  include GLEventListener
  def getGLListener
    self
  end
  

  
  def init(drawable)
     gl = drawable.getGL
    glut = GLUT.new

    gl.glShadeModel(GL::GL_SMOOTH);
    
    gl.glEnable(GL::GL_LIGHTING)
    gl.glEnable(GL::GL_LIGHT0)

    gl.glEnable(GL::GL_CULL_FACE)
  end
  
  def reshape(drawable, x, y, width, height)
  
  end

  def displayChanged(drawable, modeChanged, deviceChanged)
    
  end
  def display(drawable)
      gl = drawable.getGL();
      glut = GLUT.new
      gl.glRotated(0.15, 0, 1, 0);  
      gl.glRotated(0.15, 1, 1, 0);  
      gl.glClear(GL::GL_COLOR_BUFFER_BIT);
      gl.glColor4d(1,0,0,0);
      
      mat_specular = glFloatArray [1.0, 1.0, 1.0, 1.0]
      mat_shininess = glFloatArray [50.0]
      light_position = glFloatArray [1.0, 1.0, 1.0, 0.0]

      gl.glMaterialfv(GL::GL_FRONT, GL::GL_SPECULAR, mat_specular);  
      gl.glMaterialfv(GL::GL_FRONT, GL::GL_SHININESS, mat_shininess);
      gl.glLightfv(GL::GL_LIGHT0, GL::GL_POSITION, light_position);
      
      
      glut.glutSolidSphere 0.7, 20, 20
      gl.glFlush();
  end
  
  def glFloatArray(array)
    ret = java.nio.FloatBuffer.allocate(array.length)
    array.each do |n|
      ret.put(n)
    end
    ret
  end
  
end
